package jitobject;

import java.io.*;

import repository.Repository;
import sha1.SHA1;
import zlib.ZLibUtils;

public class Blob extends JitObject {
    public Blob() {
    }

    /**
     * Constructing blob object from a file
     *
     * @param file
     * @throws Exception
     */
    public Blob(File file) throws Exception {
        fmt = "blob";
        mode = "100644";
        value = getValue(file);
        key = genKey(file);
    }

    /**
     * Deserialize a blob object from an existed hash file in .jit/objects.
     *
     * @param Id Blob对象的key
     * @throws IOException 抛出IO异常
    */
    public static Blob deserialize(String Id) throws IOException, ClassNotFoundException {

        String parentPath = path + File.separator + Id.substring(0, 2);
        String filename = Id.substring(2);
        String path = parentPath + File.separator + filename;
        FileInputStream fileInputStream = new FileInputStream(path);
        byte[] content = ZLibUtils.decompress(fileInputStream);
        InputStream inputStream = new ByteArrayInputStream(content);

        ObjectInputStream obj = new ObjectInputStream(inputStream);

        return (Blob) obj.readObject();
    }

    /**
     * Generate key from file.
     *
     * @param file
     * @throws Exception
     */
    private String genKey(File file) throws Exception {
        String content = "100644 blob" + " " + value;
        key = SHA1.getHash(content);
        return key;
    }

    @Override
    public String toString() {
        return "100644 blob " + key;
    }
}
